package com.maoye.ossp.biz.statement.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.maoye.ossp.biz.statement.entity.BizStatementPaymentDetail;
import com.maoye.ossp.biz.statement.entity.BizStatementStmCounter;
import com.maoye.ossp.biz.statement.entity.BizStatementStmError;
import com.maoye.ossp.biz.statement.entity.BizStatementStmSelfrun;
import com.maoye.ossp.biz.statement.service.IBizStatementPaymentDetailService;
import com.maoye.ossp.biz.statement.service.IBizStatementStmErrorService;
import com.maoye.ossp.biz.statement.service.IBizStatementStmSelfrunService;
import com.maoye.ossp.biz.statement.util.BizStatementConstant;
import com.maoye.ossp.common.web.BaseController;

/**
 * 结算对账单-自营代销 Controller
 * 
 * @author yangyf
 * 
 *         2014年2月17日
 */
@Controller
@RequestMapping(value = "statement/stm/selfrun")
public class BizStatementStmSelfrunController extends BaseController {
	@Resource
	private IBizStatementStmSelfrunService stmSelfrunService;

	@Resource
	private IBizStatementPaymentDetailService paymentDetailService;

	@Resource
	private IBizStatementStmErrorService stmErrorService;

	/**
	 * 界面显示
	 * 
	 * @param stmSelfrun
	 *            参数
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "list")
	public String list(BizStatementStmSelfrun stmSelfrun, Model model,
			HttpServletRequest request) {

		Map map = new HashMap();
		map.put("stmSelfrun", stmSelfrun);
		List<BizStatementStmSelfrun> list = stmSelfrunService.queryList(map);
		model.addAttribute("list", list);
		return resultPage("/biz/statement/selfrun/list", model, request);
	}

	/**
	 * 查看详细信息
	 * 
	 * @param billNo
	 *            参数
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{billNo}/getSelfrunBillNo")
	public String getSelfrunBillNo(@PathVariable String billNo,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {

		BizStatementStmSelfrun stmSelfrun = stmSelfrunService
				.getByBillSelf(billNo);

		model.addAttribute("stmSelfrun", stmSelfrun);
		return "biz/statement/selfrun/view";
	}

	/**
	 * 扣款明细 显示
	 * 
	 * @param billNo
	 *            参数
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{billNo}/getPayment")
	public String getPayment(@PathVariable String billNo,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		BizStatementPaymentDetail paymentDetail = paymentDetailService
				.getDetailBill(billNo);
		model.addAttribute("paymentDetail", paymentDetail);
		return "/biz/statement/selfrun/paymentView";
	}

	/**
	 * 根据供应商、结算日期、是否对账 查询
	 * 
	 * @param bizStatementStmSelfrun
	 *            参数
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "getSelfrun")
	public String getSelfrun(BizStatementStmSelfrun bizStatementStmSelfrun,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Map map = new HashMap();
		map.put("isReconciliation",
				bizStatementStmSelfrun.getIsReconciliation());
		map.put("startTime", bizStatementStmSelfrun.getStartTime());
		map.put("endTime", bizStatementStmSelfrun.getEndTime());
		map.put("supplierNo", bizStatementStmSelfrun.getSupplierNo());
		List<BizStatementStmSelfrun> list = stmSelfrunService.getSelfrun(map);
		model.addAttribute("list", list);
		return "biz/statement/selfrun/list";

	}

	/**
	 * 确认结算对账
	 * 
	 * @param request
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "{billNo}/confirmSelfrun")
	public String confirmSelfrun(@PathVariable String billNo,
			HttpServletRequest request, RedirectAttributes redirectAttributes) {
		BizStatementStmSelfrun statementStmSelfrun = stmSelfrunService
				.getByBillSelf(billNo);
		Map map = new HashMap();
		map.put("id", statementStmSelfrun.getId());
		map.put("isReconciliation", BizStatementConstant.IS_RECONCILIATION);
		stmSelfrunService.confirmSelfrun(map);
		addMessage(redirectAttributes, "结算对账成功");
		return "biz/statement/selfrun/list";
	}

	/**
	 * 添加专柜异常信息
	 * 
	 * @param statementStmError
	 *            参数
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "{billNo}/add")
	public String add(@PathVariable String billNo,
			BizStatementStmError statementStmError, HttpServletRequest request,
			HttpServletResponse response, Model model) {

		BizStatementStmSelfrun stmSelfrun = stmSelfrunService
				.getByBillSelf(billNo);

		statementStmError
				.setStatementType(BizStatementConstant.STATEMENT_TYPE_1);

		model.addAttribute("stmCounter", stmSelfrun);

		model.addAttribute("statementStmError", statementStmError);

		return "/biz/statement/counter/counterErrorAdd";

	}

	/**
	 * 保存添加的异常信息
	 * 
	 * @param stmError
	 * @param request
	 * @param response
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "{billNo}/save")
	public String save(@PathVariable String billNo,
			BizStatementStmError statementStmError, HttpServletRequest request,
			HttpServletResponse response, Model model) {

		BizStatementStmSelfrun statementStmSelfrun = stmSelfrunService
				.getByBillSelf(billNo);
		Long id = idGenerator.getUId();
		statementStmError.setId(id);

		statementStmError.setIsConfirmed(BizStatementConstant.UN_CONFIRMED);
		statementStmError.setCreateTime(statementStmSelfrun.getStartTime());
		statementStmError.setDate(statementStmSelfrun.getEndTime());
		statementStmError
				.setSupplierName(statementStmSelfrun.getSupplierName());
		statementStmError.setErrorAmount(statementStmSelfrun.getAmountPaid());
		statementStmError.setActualAmount(statementStmSelfrun.getActualCost());

		Map map = new HashMap();
		map.put("statementStmError", statementStmError);

		stmErrorService.create(statementStmError);
		List<BizStatementStmError> list = stmErrorService.getErrorList(map);

		model.addAttribute("list", list);
		return "/biz/statement/counter/counterError";
	}

}
